#include "check.h"

unsigned char CBoxRowIndex(grille g, unsigned char index)
{
  unsigned char n = GgetNumberIndex(g, index);
  
  if( n > 0 )
  {
    unsigned char i = index - index % 9;
    unsigned char imax = i + 9;

    while( i < imax )
    {
      if( i != index )
      {
        if( GgetNumberIndex(g, i) == n )
          return 0;
      }
      i++;
    }

    return 1;
  }
  else
    return 1;
}

unsigned char CBoxRow(grille g, unsigned char x, unsigned char y)
{
  return CBoxRowIndex(g, Gindex(x,y));
}

unsigned char CBoxColIndex(grille g, unsigned char index)
{
  unsigned char n = GgetNumberIndex(g, index);

  if( n > 0 )
  {
  
    unsigned char i = index % 9;

    while( i <= 80 )
    {
      if( i != index )
      {
        if( GgetNumberIndex(g, i) == n )
          return 0;
      }
      i += 9;
    }
    return 1;
  }
  else
    return 1;
}

unsigned char CBoxCol(grille g, unsigned char x, unsigned char y)
{
  return CBoxColIndex(g, Gindex(x, y));
}

unsigned char CBoxSquareIndex(grille g, unsigned char index)
{
  unsigned char n = GgetNumberIndex(g, index);

  if( n > 0 )
  {
    unsigned char i = (index % 9) - (index % 9) % 3 + 27 * (index > 26) + 27 * (index > 53);
    unsigned char k,l;

    for( k = 0; k < 3 ; k++ )
    {
      for( l = 0; l < 3 ; l++ )
      {
        if( i != index )
        {
          if( GgetNumberIndex(g, i) == n )
            return 0;
        }
        i++;
      }
      i += 6;
    }
    return 1;
  }
  else
    return 1;
}

unsigned char CBoxSquare(grille g, unsigned char x, unsigned char y)
{
  return CBoxSquareIndex(g, Gindex(x, y));
}

unsigned char CBoxAll(grille g, unsigned char x, unsigned char y)
{
  if( CBoxRow(g, x, y) == 1 &&
      CBoxCol(g, x, y) == 1 &&
      CBoxSquare(g, x, y) == 1 )
    return 1;
  else
    return 0;
}

unsigned char CBoxAllIndex(grille g, unsigned char i)
{
  if( CBoxRowIndex(g, i) == 1 &&
      CBoxColIndex(g, i) == 1 &&
      CBoxSquareIndex(g, i) == 1 )
    return 1;
  else
    return 0;
}


unsigned char CRow(grille g, unsigned char rowNb)
{
  unsigned char i = (rowNb-1)*9;
  unsigned char imax = i+9;
  unsigned char nb;

  unsigned char t[10] = {0};

  for( ; i <= imax ; i++ )
  {
    nb = GgetNumberIndex(g, i);
    if( nb > 0 )
    {
      if( t[nb] == 1 )
        return 0;
      else
        t[nb] = 1;
    }
  }
  return 1;
}

unsigned char CCurrentBox(grille g)
{
  return CBoxAll(g, GgetPosX(g), GgetPosY(g));
}

unsigned char CCol(grille g, unsigned char colNb)
{
  unsigned char i = colNb-1;
  unsigned char nb;

  unsigned char t[10] = {0};

  for( ; i < 81 ; i += 9 )
  {
    nb = GgetNumberIndex(g, i);
    if( nb > 0 )
    {
      if( t[nb] == 1 )
        return 0;
      else
        t[nb] = 1;
    }
  }
  return 1;
}

unsigned char CSquare(grille g, unsigned char squareNb)
{
  unsigned char i;
  switch(squareNb)
  {
    case 1:
      i = 0;
      break;
    case 2:
      i = 3;
      break;
    case 3:
      i = 6;
      break;
    case 4:
      i = 27;
      break;
    case 5:
      i = 30;
      break;
    case 6:
      i = 33;
      break;
    case 7:
      i = 54;
      break;
    case 8:
      i = 57;
      break;
    case 9:
      i = 60;
      break;
    default:
      i = 0;
  }

  unsigned char nb;
  unsigned char t[10] = {0};

  unsigned k,l;
  for( k = 0; k < 3; k++ )
  {
    for( l = 0; l < 3; l++ )
    {
      nb = GgetNumberIndex(g, i);
      if( nb > 0 )
      {
        if( t[nb] == 1 )
          return 0;
        else
          t[nb] = 1;
      }
      i++;
    }
    i += 6;
  }

  return 1;
}

unsigned char CAll(grille g)
{
  unsigned char i;
  for( i = 1; i < 10; i++ )
  {
    if( CRow(g, i) == 0 ||
        CCol(g, i) == 0 ||
        CSquare(g, i) == 0 )
      return 0;
  }
  return 1;
}
